﻿using System;
using System.Collections.ObjectModel;
using System.Text;
using System.Text.RegularExpressions;

namespace AEA.RSSFeedReaderLib
{
    /// <summary>
    /// Used to hold data relating to individual feed items belonging to a channel of an RSS Feed
    /// </summary>
    [Serializable]
    public class ItemFeed
    {
        #region Class Members
        private string _summary;
        #endregion

        #region Constructor
        /// <summary>
        /// Initialize a new instance of the ItemFeed class
        /// </summary>
        public ItemFeed() { }

        /// <summary>
        /// Initialize a new instance of the ItemFeed class
        /// </summary>
        /// <param name="title">The title of the item feed</param>
        /// <param name="summary">The summary of the item feed</param>
        /// <param name="link">The link to the article of the item feed</param>
        public ItemFeed(string title, string summary, string link)
        {
            summary = StripHtml(summary);
            if (summary.Length > 500)
                summary = summary.Substring(0, 500) + "...";

            Title = title;
            _summary = summary;
            Link = link;
        }
        #endregion

        #region Properties
        /// <summary>
        /// The Title of the RSS Item
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// The Link to the article
        /// </summary>
        public string Link { get; set; }

        /// <summary>
        /// Summary of the feed
        /// </summary>
        public string Summary 
        {
            get
            {
                return _summary;
            }

            set
            {
                _summary = value;
            }
        }
        #endregion

        #region Class Methods
        static string StripHtml(string html)
        {
            if (String.IsNullOrEmpty(html))
                return String.Empty;

            // Get rid of any HTML-encoded characters, like converting &lt; to '<'
            string decodedHtml = System.Web.HttpUtility.HtmlDecode(html);

            // Remove all <open> and </close> tags.
            StringBuilder text = new StringBuilder(Regex.Replace(decodedHtml, "<[^>]*>", String.Empty));

            const string TWO_SPACES = "  ";
            const string ONE_SPACE = " ";

            text.Replace(TWO_SPACES, ONE_SPACE);
            text.Replace("\t", ONE_SPACE);
            text.Replace("\n", ONE_SPACE);

            return text.ToString();
        }
        #endregion

        #region Base Class Overrides
        public override string ToString()
        {
            return Title;
        }
        #endregion
    }

    /// <summary>
    /// Collection for ItemFeeds
    /// </summary>
    [Serializable]
    public class ItemFeedCollection : ObservableCollection<ItemFeed>
    {
    }
}
